<%@ page contentType="text/html;charset=UTF-8"%>
<script type="text/javascript">
	//是否服务器分页
	var _isServerPage = true;
	//表格列定义
	var _tableCols = 	[{
		data : "uuid",
		orderable : false,
		className : "center",
		width : "30",
		render : RapDataTableUtils.renderCheckCol
	},
	{
		data : null,
		width : "120",
		render : function(data, type, full) {
			var content = data.property==null?"":data.property.theName;
			return RapDataTableUtils.renderStrByLength(content,24);
		},
		orderable : false
	},
	{
		data : 'theName',
		width : "120",
		render : function(data, type, full) {
			return RapDataTableUtils.renderStrByLength(data,24);
		},
		orderable : true
	},
	{
		data : null,
		width : "50",
		className :"rap-td-number",
		render:function (data, type, full){
			var content = data.property==null?"":data.property.totalArea;
			return milliFormat(content);
		},
		orderable : false
	},
	{
		data : 'area',
		width : "50",
		className :"rap-td-number",
		render:milliFormat,
		orderable : true
	},
	{
		data : 'region',
		width : "50",
		orderable : true,
		render : function(data, type, full) {
			return RapDataUtils.getEnumName("stwy_region", data);
		}
	},
	{
		data : 'usefor',
		width : "50",
		orderable : true,
		render : function(data, type, full) {
			return RapDataUtils.getEnumName("stwy_usefor", data);
		}
	},
	{
		data : 'assetState',
		width : "50",
		render : renderState,
		orderable : true
	},
	{
		data : 'bookType',
		width : "80",
		render : function(data, type, full) {
			return RapDataUtils.getEnumName("stwy_bookType", data);
		},
		orderable : true
	},
	{
		data : 'uuid',
		width : "50",
		render : function(data, type, full) {
			var url = "${ctx}/frame/attachment/list?entityType=AssetEntity&entityUuid="+data;
			<shiro:lacksPermission  name="stwy:pro:asset:update">
			url = url+"&editAble=false";//没有修改权限
			</shiro:lacksPermission >
			url = url +"&callback=callbackSaveFile";
			var show = "";
			if(full.fileCount==null||full.fileCount==0){
				show = "未上传";
			}else{
				show = full.fileCount;
			}
			return "<a href='#' onclick='openWin(this,\""+url+"\");'>"+show+"</a>";
		},
		orderable : false
	},
	{
		data : 'theCode',
		width : "50",
		render : function(data, type, full) {
			var url = "${ctx}/pro/asset/showHistory?theCode="+data;
			return "<a href='#' onclick='showHistory(this,\""+url+"\");'>变更记录</a>";
		},
		orderable : false
	},
	{
		data : "uuid",
		className : "center",
		orderable : false,
		render : function(data, type, full) {
			return "<div class='rap-btn-actiongroup'>" 
			+ "<button id='rap-btn-view-row' class='btn btn-xs btn-success' data-rel='tooltip' title='查看'><i class='fa fa-search-plus'></i></button>"
			<shiro:hasPermission name="stwy:pro:asset:update">
			+ "<button id='rap-btn-edit-row' class='btn btn-xs btn-info' data-rel='tooltip' title='编辑'><i class='fa fa-edit'></i></button>"
			</shiro:hasPermission>
			<shiro:hasPermission name="stwy:pro:asset:delete">
			+ "<button id='rap-btn-remove-row' class='btn btn-xs btn-danger' data-rel='tooltip' title='删除'><i class='fa fa-trash-o'></i></button>"
			</shiro:hasPermission>
			+ "</div>";
		},
		width : "80"
	}]
;
	function callbackSaveFile(entityType,entityUuid){
		$.ajax({
			url : "${serviceurl}/updateAssetFileState",
			data : {"entityType":entityType,"entityUuid":entityUuid},
			dataType : 'json',
			success : function(data) {
				if(data.success){
					onRefresh();
				}else{
					alert(data.msg);
				}
			}
		});
	}
	function renderState(data, type, full) {
		var stateName = RapDataUtils.getEnumName("stwy_assetStatus", data);
		var color="";
		if("0"==data){
			color="#f13e46";
		}else if("1"==data){
			color="grey";
		}else if("2"==data){
			color="#6FB3E0";
		}else if("3"==data){
			color="#FFC125";
		} 
		stateName = "<span style='background-color:"+color+";color:#ffffff;padding:2px 5px;'>"+stateName+"</span>";
		return stateName;
	}
	function showHistory(obj,url){
		$(obj).colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : url,
			innerWidth : '80%',
			innerHeight : '80%',
			overlayClose : true,
			close : '×',
			fixed : true
		}); 
	}
	function openWin(obj,url){
		/* $(obj).colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : url,
			innerWidth : '90%',
			innerHeight : '85%',
			close : '×',
			fixed : true
		}); */
		window.open(url,'file');
	}
	//宏：生成ShowData方法，在编辑框显示记录数据
	function showData(data) {
		$("input[name='uuid']").val(data.uuid);
		$("input[name='theCode']").val(data.theCode);
		$("input[name='theName']").val(data.theName);
		$("input[name='assetState']").val(data.assetState);
		$("input[name='area']").val(data.area);
		$("select[name='region']").val(data.region);
		$("select[name='usefor']").val(data.usefor);
		$("select[name='groupOne']").val(data.groupOne);
		$("select[name='groupTwo']").val(data.groupTwo);
		$("input[name='houseCert']").val(data.houseCert);
		$("input[name='address']").val(data.address);
		$("textarea[name='remark']").val(data.remark);
		$("input[name='property.uuid']").val(data.property.uuid);
		$("input[name='property.theName']").val(data.property.theName);
		$("input[name='propertyName']").val(data.property.theName);
		
		$("select[name='bookType']").val(data.bookType);
	}
	//绑定除了默认事件外的其他事件
	function bindOtherAction(){
		$("#rap-form-edit input[name='theName']").bind('blur',function(){
			checkRepeat(this,true);
		});
		$("#rap-btn-ref-property").colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : "${ctx}/pro/property/select?search_EQ_status=1&callback=callbackProperty",
			innerWidth : '70%',
			innerHeight : '80%',
			close : '×',
			fixed : true
		});
		$('#propertyName').bind('focus', function(){
			$("#rap-btn-ref-property").click();
		});
		$("#rap-btn-hasdeleted").colorbox({
			iframe : true,
			opacity	: 0.2,
			transition : 'none',
			speed : 200,
			href : "${serviceurl}/hasdeleted",
			innerWidth : '70%',
			innerHeight : '80%',
			close : '×',
			fixed : true
		});
		
	}
	function callbackProperty(data){
		$("#propertyId").val(data.uuid);
		$("#propertyName").val(data.theName);
	}
	$(document).ready(function() {
		bindDefaultActions();
		bindOtherAction();
		_queryData = $("#rap-form-query").serializeArray();
		if(_isServerPage){
			//服务器分页
			serverPage();	
		}else{
			//非服务器分页
			_tableList = $('#rap-table-list').DataTable({
				"columns" : _tableCols,
				"createdRow" : RapDataTableUtils.setActions,
				"order": [] 
			});
			loadList();		
		}
		
		RapDataTableUtils.setCheckCol(_tableList);

		/**
		 * 表单校验
		 */
		_validator = $('#rap-form-edit').validate({
					rules : {
						propertyName :{
							required : true
						},
						theName : {
							required : true
						},
						area :{
							required : true,
							isFloat : true
						},
						region :{
							required : true
						},
						groupOne :{
							required : true
						},
						groupTwo :{
							required : true
						},
						usefor :{
							required : true
						}
					},
					messages : {
						area :{
							isFloat : "请输入数字"
						}
					}
				});
	});
</script>